package com.wukaige.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wukaige.user.conmon.JwtUtils;
import com.wukaige.user.conmon.ResponseResult;
import com.wukaige.user.conmon.ResultEntity;
import com.wukaige.user.entity.User;
import com.wukaige.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wukaige
 * @since 2021-05-05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @RequestMapping("loginuser")
    public ResultEntity loginuser(@RequestBody User user) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", user.getUsername());
        User users = userService.getOne(wrapper);
        if (users == null) {
            return ResultEntity.error("500", "账号或密码错误");
        }
        if (users.getPassword().equals(user.getPassword())) {
            JwtUtils jwtUtils = new JwtUtils();

            String token = jwtUtils.generateToken((long) users.getUid());

            return ResultEntity.ok("200", "登陆成功", token);
        } else {
            return ResultEntity.error("500", "密码错误");
        }
    }

    @RequestMapping("adduser")
    public ResponseResult adduser(@RequestBody User user) {
        boolean save = userService.save(user);
        return ResponseResult.success(save);
    }

    //查询用户名字
    @RequestMapping("getunamebyuid")
    public User getunamebyuid(@RequestBody Integer uid) {
        User byId = userService.getById(uid);
        return byId;
    }

}

